﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;

namespace 约苗秒杀.Service
{
    public static class HttpHelper
    {

        public static T Post<T>(string url, object postData, string token = "") where T : class
        {
            try
            {
                string postData2 = string.Empty;
                if (postData != null)
                {
                    postData2 = ((!(postData is string)) && postData != null ? JsonConvert.SerializeObject(postData) : (postData as string));
                }
                Dictionary<string, string> list = null;
                if (!string.IsNullOrEmpty(token))
                { 
                    list = new Dictionary<string, string>();
                    list.Add("token", token);
                }
                return JsonConvert.DeserializeObject<T>(HttpPost(url, postData2, list));
            }
            catch (HttpRequestException ex)
            {

            }
            return null;
        }

        public static T Get<T>(string url, string token = "") where T : class
        {
            Dictionary<string, string> list = null;
            if (!string.IsNullOrEmpty(token))
            {
                list = new Dictionary<string, string>();
                list.Add("token", token);
            }
            return JsonConvert.DeserializeObject<T>(HttpGet(url, null, list));
        }

        public static void AddTokenToHeader(this HttpWebRequest request, Dictionary<string, string> headerKVs)
        {
            if (request.Headers == null)
            {
                request.Headers = new WebHeaderCollection();
            }
            if (headerKVs != null)
            {
                foreach (var headerKV in headerKVs)
                {
                    if (headerKV.Key.Contains("User-Agent"))
                    {
                        request.UserAgent = headerKV.Value;
                        continue;
                    }
                    if (headerKV.Key.Contains("Accept"))
                    {
                        request.Accept = headerKV.Value;
                        continue;
                    }
                    if (headerKV.Key.Contains("Host"))
                    {
                        request.Host = headerKV.Value;
                        continue;
                    }
                    if (headerKV.Key.Contains("Referer"))
                    {
                        request.Referer = headerKV.Value;
                        continue;
                    }
                    request.Headers.Add(headerKV.Key, headerKV.Value);
                }
            }
        }

        public static string HttpGet(string Url, string postDataStr, Dictionary<string, string> headerKVs = null)
        {
            try
            {
                HttpWebRequest obj = (HttpWebRequest)WebRequest.Create(Url + (string.IsNullOrEmpty(postDataStr) ? "" : "?") + postDataStr);
                obj.Method = "GET";
                obj.ContentType = "text/html;charset=UTF-8";
                obj.AddTokenToHeader(headerKVs);
                using (Stream stream = ((HttpWebResponse)obj.GetResponse()).GetResponseStream())
                {
                    using (StreamReader streamReader = new StreamReader(stream, Encoding.GetEncoding("utf-8")))
                    {
                        string result = streamReader.ReadToEnd();
                        streamReader.Close();
                        stream.Close();
                        return result;
                    }
                }
            }
            catch (Exception ex)
            {

                return ex.Message;
            }
        }

        public static string HttpPost(string posturl, string postData = "", Dictionary<string, string> headerKVs = null)
        {
            try
            {
                Stream stream = null;
                StreamReader streamReader = null;
                Encoding encoding = Encoding.GetEncoding("utf-8");
                byte[] bytes = encoding.GetBytes(postData);
                HttpWebRequest obj = WebRequest.Create(posturl) as HttpWebRequest;
                CookieContainer cookieContainer2 = obj.CookieContainer = new CookieContainer();
                obj.AllowAutoRedirect = true;
                obj.AddTokenToHeader(headerKVs);
                obj.Method = "POST";
                obj.ContentType = "application/json";
                obj.ContentLength = bytes.Length;
                Stream requestStream = obj.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
                using (stream = (obj.GetResponse() as HttpWebResponse).GetResponseStream())
                {
                    using (streamReader = new StreamReader(stream, encoding))
                    {
                        string result = streamReader.ReadToEnd();
                        string empty = string.Empty;
                        return result;
                    }
                }
            }
            catch (HttpListenerException ex)
            {

                return ex.Message;
            }
        }
    }
}
